//
//  MCLoginViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/1.
//  Copyright © 2017年 TLYL01. All rights reserved.
//



#import "MCLoginViewController.h"
#import "MCWaterRippleButton.h"
#import "MCMainTabBarController.h"
#import "MCLoginModel.h"
#import "MCMineInfoModel.h"
#import "MCLotteryID.h"
#import "MCGetMerchantInfoModel.h"
#import "MCKefuViewController.h"

@interface MCLoginViewController ()
//用户名输入框
@property (nonatomic,weak) UITextField *userNameTF;
//密码输入框
@property (nonatomic,weak) UITextField *passWordTF;
//登录按钮
@property (nonatomic,weak) MCWaterRippleButton *loginBtn;
//底部ScrollView
@property (nonatomic,weak) UIScrollView *baseScrollV;
//loginModel登录模型的强引用
@property (nonatomic,strong) MCLoginModel *loginModel;
//getMerchantInfoModel登录模型的强引用
@property (nonatomic,strong) MCGetMerchantInfoModel *getMerchantInfoModel;


@end

@implementation MCLoginViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setUpUI];
    
}

- (void)setUpUI{
    
    CGFloat padding = MC_REALVALUE(76);
    UIScrollView *baseScrolV = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:baseScrolV];
    self.baseScrollV = baseScrolV;
    self.view.backgroundColor = RGB(6, 34, 50);
    UIImageView *logoImageV = [[UIImageView alloc] init];
    
    logoImageV.image = [UIImage imageNamed:@"Logo"];
    logoImageV.layer.masksToBounds = YES;
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [baseScrolV addSubview:bgImageView];
    [bgImageView addSubview:logoImageV];
   
    bgImageView.backgroundColor = [UIColor whiteColor];
    bgImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapGestureRecognizer)];
    [bgImageView addGestureRecognizer:tap];
    [logoImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(baseScrolV);
        make.top.equalTo(baseScrolV).offset(MC_REALVALUE(105));
        make.height.equalTo(@(MC_REALVALUE(54)));
        make.width.equalTo(@(MC_REALVALUE(71)));
    }];
    
    UITextField *userNameTF = [[UITextField alloc] init];
    [bgImageView addSubview:userNameTF];
    userNameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入用户名" attributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor],NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(15)]}];
    userNameTF.font = [UIFont systemFontOfSize:MC_REALVALUE(15)];
    [userNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgImageView.mas_left).offset(padding + 55);
        make.right.equalTo(bgImageView.mas_right).offset(-padding);
        make.top.equalTo(logoImageV.mas_bottom).offset(MC_REALVALUE(95));
        make.height.equalTo(@(MC_REALVALUE(50)));
    }];
    
    userNameTF.autocorrectionType = UITextAutocorrectionTypeNo;
    userNameTF.textColor = MC_ColorWithAlpha(255, 255, 255, 0.95);
    UIView *lineName = [[UIView alloc] init];
    [bgImageView addSubview:lineName];
    userNameTF.textColor = RGB(68, 68, 68);
    UIImageView *imgName = [[UIImageView alloc] init];
    [baseScrolV addSubview:imgName];
    imgName.image = [UIImage imageNamed:@"user-icon"];
    [imgName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(userNameTF.mas_left).offset(-MC_REALVALUE(30));
        make.centerY.equalTo(userNameTF.mas_centerY);
        make.height.width.equalTo(@(MC_REALVALUE(25)));
    }];
    lineName.backgroundColor = RGB(189, 189, 189);
    [lineName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userNameTF.mas_bottom);
        make.left.equalTo(bgImageView).offset(padding);
        make.right.equalTo(bgImageView).offset(-padding);
        make.height.equalTo(@(0.5));
    }];
    self.userNameTF = userNameTF;
    UITextField *passWordTF = [[UITextField alloc] init];
    [bgImageView addSubview:passWordTF];
    passWordTF.font = [UIFont systemFontOfSize:MC_REALVALUE(15)];
    passWordTF.autocorrectionType = UITextAutocorrectionTypeNo;
    passWordTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor],NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(15)]}];
    
    UIButton *lineBtn = [[UIButton alloc] init];
    [lineBtn setTitle:@"线路检测" forState:UIControlStateNormal];
    [lineBtn addTarget:self action:@selector(lineBtnClick) forControlEvents:UIControlEventTouchDown];
    lineBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(15)];
    [lineBtn setTitleColor:RGB(79, 176, 222) forState:UIControlStateNormal];
    [baseScrolV addSubview:lineBtn];
    UIButton *kefuBtn = [[UIButton alloc] init];
    [kefuBtn setTitle:@"联系客服" forState:UIControlStateNormal];
    [kefuBtn addTarget:self action:@selector(kefuBtnClick) forControlEvents:UIControlEventTouchDown];
    kefuBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(15)];
    [kefuBtn setTitleColor:RGB(79, 176, 222) forState:UIControlStateNormal];
    [baseScrolV addSubview:kefuBtn];
    UIButton *forgetBtn = [[UIButton alloc] init];
    [forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchDown];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(15)];
    [forgetBtn setTitleColor:RGB(79, 176, 222) forState:UIControlStateNormal];
    [baseScrolV addSubview:forgetBtn];
    [passWordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userNameTF.mas_left);
        make.right.equalTo(userNameTF.mas_right);
        make.top.equalTo(userNameTF.mas_bottom);
        make.height.equalTo(userNameTF.mas_height);
    }];

    passWordTF.textColor = RGB(68, 68, 68);
    UIView *linePass = [[UIView alloc] init];
  
    [bgImageView addSubview:linePass];
    linePass.backgroundColor = RGB(189, 189, 189);
    [linePass mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passWordTF.mas_bottom);
        make.left.equalTo(bgImageView).offset(padding);
        make.right.equalTo(bgImageView).offset(-padding);
        make.height.equalTo(@(0.5));
    }];
    
    UIImageView *imgPsw = [[UIImageView alloc] init];

    [baseScrolV addSubview:imgPsw];
    imgPsw.image = [UIImage imageNamed:@"lock-icon"];
    [imgPsw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(passWordTF.mas_left).offset(-MC_REALVALUE(30));
        make.centerY.equalTo(passWordTF.mas_centerY);
        make.height.width.equalTo(@(MC_REALVALUE(25)));
    }];
    [userNameTF setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    [userNameTF setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [passWordTF setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    [passWordTF setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    passWordTF.secureTextEntry = YES;

    self.passWordTF = passWordTF;
    MCWaterRippleButton *loginBtn = [[MCWaterRippleButton alloc] init];
    [bgImageView addSubview:loginBtn];
    loginBtn.layer.cornerRadius = MC_REALVALUE(25);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(18)];
    [loginBtn addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    loginBtn.backgroundColor = RGB(54, 128, 211);
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"Button_Determine"] forState:UIControlStateNormal];
    [loginBtn setImage:[UIImage imageNamed:@"Button_Determin_Right"] forState:UIControlStateNormal];
    [lineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineName);
        make.top.equalTo(linePass.mas_bottom).offset(MC_REALVALUE(33));
        
    }];
    [kefuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineBtn.mas_right).offset(MC_REALVALUE(23));
        make.top.equalTo(lineBtn);
        
    }];
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(kefuBtn.mas_right).offset(MC_REALVALUE(23));
        make.top.equalTo(lineBtn);
        
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgName.mas_left);
        make.right.equalTo(userNameTF.mas_right);
        make.top.equalTo(forgetBtn.mas_bottom).offset(MC_REALVALUE(33));
        make.height.equalTo(userNameTF.mas_height);
    }];
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}


#pragma mark - touch event


- (void)lineBtnClick{
    
    
}
- (void)kefuBtnClick{
    
    MCKefuViewController *vc = [[MCKefuViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (void)forgetBtnClick{
    
    
}

- (void)imageViewTapGestureRecognizer{
    
    [self.view endEditing:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)loginButtonClick:(UIButton *)btn{
//    
//    [self.view endEditing:YES];
//    
//    if([self.userNameTF.text  isEqualToString: @""]){
//       
//        [SVProgressHUD showErrorWithStatus:@"用户名不能为空"];
//        return;
//    }
//    if ([self.passWordTF.text isEqualToString:@""]) {
//        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
//        return;
//    }
    
//    MCLoginModel *loginModel = [[MCLoginModel alloc]initWithUserName:self.userNameTF.text passWord:self.passWordTF.text];
//    self.loginModel = loginModel;
//
//    [loginModel refreashDataAndShow];
// 
//    __weak __typeof(self)wself = self;
//    loginModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
//
//        [wself shakeAnimationForView:wself.baseScrollV];
//
//    };
//    loginModel.callBackSuccessBlock = ^(ApiBaseManager *manager) {
//
//        if (wself.presented == YES) {
//            [wself dismissViewControllerAnimated: YES completion:nil];
//        } else {
            [[[UIApplication sharedApplication] keyWindow] setRootViewController:[[MCMainTabBarController alloc] init]];
            
//        }
//
//        MCGetMerchantInfoModel *model = [MCGetMerchantInfoModel sharedMCGetMerchantInfoModel];
//        
//        [model refreashDataAndShow];
//        self.getMerchantInfoModel = model;
//        model.callBackFailedBlock = ^(id manager, NSString *errorCode) {
//            
//            
//        };
//        
//        model.callBackSuccessBlock = ^(id manager) {
//            
//        };
//    };
// 
}


- (void)shakeAnimationForView:(UIView *) view

{
    CALayer *viewLayer = view.layer;
    
    CGPoint position = viewLayer.position;
    
    CGPoint x = CGPointMake(position.x + 5, position.y);
    
    CGPoint y = CGPointMake(position.x - 5, position.y);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    
    [animation setAutoreverses:YES];
    
    [animation setDuration:.03];
    
    [animation setRepeatCount:4];
    
    [viewLayer addAnimation:animation forKey:nil];
    
}

@end
